package com.fann;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class JoinControllServle
 */
public class JoinControllerServle extends HttpServlet {
	private static final long serialVersionUID = 1L;

    private JoinDAO dao;
    /**
     * @see HttpServlet#HttpServlet()
     */
    public JoinControllerServle() {
        super();
        dao = new JoinDAO();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		HttpSession session = request.getSession();
		String acttype = request.getParameter("acttype");
		
		if (acttype.equals("joinList")){
			List<Join> JOs = dao.getAll();
            request.setAttribute("JOs", JOs); 
            
            session.setAttribute("left", "/Join/joinTree.jsp");
			session.setAttribute("center", "/Join/joinList.jsp");
			
		} else if (acttype.equals("joinAdd")){
			session.setAttribute("center", "/Join/joinEdit.jsp");

		} else if (acttype.equals("joinEdit")){
			
			Join JO = dao.getJoinById(request.getParameter("id"));
			session.setAttribute("JO", JO);
			
			session.setAttribute("center", "/Join/joinEdit.jsp");
			
		} else if (acttype.equals("joinDelete")){
			
			int rowCount =0;
			
			String id = request.getParameter("id");
			if (id !=null){
				rowCount = dao.delete(id);
			}
			
			session.setAttribute("center", "/Join/joinDelete.jsp?rowCount="+ rowCount);
			
		} 

		request.getRequestDispatcher("default.jsp").forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		request.setCharacterEncoding("UTF-8");
		String acttype = request.getParameter("acttype");
		
		String jo_id, user_id, jo_Sdate, jo_Edate, cr_id, lo_id, ls_id, cs_id, op_Sdate, op_Edate;
		int id;
		
		if (acttype.equals("joinAdd")){
			if (request.getParameter("jo_id") != null){
				
				id =0;
				jo_id = request.getParameter("jo_id");
				user_id = request.getParameter("user_id");
				jo_Sdate = request.getParameter("jo_Sdate");
				jo_Edate = request.getParameter("jo_Edate");
				
				cr_id = request.getParameter("cr_id");
				lo_id = request.getParameter("lo_id");
				ls_id = request.getParameter("ls_id");
				cs_id = request.getParameter("cs_id");
				op_Sdate = request.getParameter("op_Sdate");
				op_Edate = request.getParameter("op_Edate");
				
				//Construction
				Join JO = new Join(id, jo_id, user_id, jo_Sdate, jo_Edate, cr_id, lo_id, ls_id, cs_id, op_Sdate, op_Edate);
				
				//add
				dao.add(JO);
				
				//Success, Fail
				response.sendRedirect("JoinController?acttype=joinList");
			} else {
				response.sendRedirect("JoinController?acttype=joinAdd");
			}
		}
		
		if (acttype.equals("joinEdit")){
			
			id = Integer.parseInt(request.getParameter("id"));
			jo_id = request.getParameter("jo_id");
			user_id = request.getParameter("user_id");
			jo_Sdate = request.getParameter("jo_Sdate");
			jo_Edate = request.getParameter("jo_Edate");
			cr_id = request.getParameter("cr_id");
			
			lo_id = request.getParameter("lo_id");
			ls_id = request.getParameter("ls_id");
			cs_id = request.getParameter("cs_id");
			op_Sdate = request.getParameter("op_Sdate");
			op_Edate = request.getParameter("op_Edate");
			
			//Construction
			Join JO = new Join(id, jo_id, user_id, jo_Sdate, jo_Edate, cr_id, lo_id, ls_id, cs_id, op_Sdate, op_Edate);
			
			//update
		    dao.update(JO);
		    
			//Set List
			List<Join> JOs = dao.getAll();
            request.setAttribute("JOs", JOs);
            
		    response.sendRedirect("JoinController?acttype=joinList");
		}
	}

}
